/*
 * Copyright (C) 2008
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.sca4idea.providers;

import com.googlecode.sca4idea.SCABundle;
import com.googlecode.sca4idea.psi.SCAComponentTagPsiElement;
import com.googlecode.sca4idea.psi.SCAJavaComponentTagPsiElement;
import com.googlecode.sca4idea.psi.SCAMockComponentTagPsiElement;
import com.googlecode.sca4idea.psi.SCAWebappComponentTagPsiElement;
import static com.googlecode.sca4idea.references.SCAReferencesUtil.componentElementDetailAttribute;
import static com.googlecode.sca4idea.references.SCAReferencesUtil.implementationBody;
import com.intellij.psi.impl.source.resolve.reference.impl.providers.JavaClassListReferenceProvider;
import com.intellij.psi.impl.source.resolve.reference.impl.providers.JavaClassReferenceProvider;
import com.intellij.psi.xml.XmlTag;

/**
 * Support for Fabric3 specific extensions.
 */
public class Fabric3SCAProvider implements SCAProvider {
    public static Fabric3SCAProvider INSTANCE = new Fabric3SCAProvider();
    private static final String F3_NS = "http://fabric3.org/xmlns/sca/2.0-alpha";

    private Fabric3SCAProvider() {
    }

    public String getId() {
        return "fabric3";
    }

    public String getDisplayName() {
        return SCABundle.message("provider.fabric3.name");
    }

    public void registerReferences(ProviderReferenceRegistrar registry) {
        // JUnit implementations
        JavaClassReferenceProvider classReferenceProvider = new JavaClassReferenceProvider(registry.getProject());
        classReferenceProvider.setSoft(false);
        registry.registerReferenceProvider(componentElementDetailAttribute("junit", "class"), classReferenceProvider);

        // Mock implementation
        JavaClassListReferenceProvider multiClassReferenceProvider = new JavaClassListReferenceProvider(registry.getProject());
        multiClassReferenceProvider.setSoft(false);
        registry.registerReferenceProvider(implementationBody("implementation.mock"), multiClassReferenceProvider);
    }

    public SCAComponentTagPsiElement getPSIElementForTag(XmlTag componentTag) {
        XmlTag[] junitImpl = componentTag.findSubTags("junit", F3_NS);
        if (junitImpl.length > 0) {
            return new SCAJavaComponentTagPsiElement(componentTag, junitImpl[0]);
        }
        XmlTag[] f3WebImpl = componentTag.findSubTags("web", F3_NS);
        if (f3WebImpl.length > 0) {
            return new SCAWebappComponentTagPsiElement(componentTag);
        }
        XmlTag[] mockImpl = componentTag.findSubTags("implementation.mock", VanillaSCAProvider.SCA_NS);
        if (mockImpl.length > 0) {
            return new SCAMockComponentTagPsiElement(componentTag, mockImpl[0]);
        }
        XmlTag[] f3MockImpl = componentTag.findSubTags("implementation.mock", F3_NS);
        if (f3MockImpl.length > 0) {
            return new SCAMockComponentTagPsiElement(componentTag, f3MockImpl[0]);
        }

        return null;
    }

    public boolean isAlwaysActive() {
        return false;
    }
}
